/*
 * Copyright (c) 2009-2011 Ricardo Quesada
 * Copyright (c) 2011-2012 Zynga Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


#import <Box2d/Box2D.h>
#import "cocos2d.h"

#import "GameConstants.h"
#import "Railing.h"
//
// Railing: A solid plaform. It uses the "solid_Railing.png" texture
//


@implementation Railing
{
    //边长，所以是指针
    CGPoint *vertexs;
    //fixture 节点数目
    int fCount;
}
-(id) initWithBody:(b2Body*)body game:(GameNode*)game
{
	if( (self=[super initWithBody:body game:game])) {
		reportContacts_ = BN_CONTACT_NONE;
		preferredParent_ = BN_PREFERRED_PARENT_PLATFORMS_SINGLE;
		isTouchable_ = NO;
		[self setAnchorPoint: ccp(0,1)];
        fCount= body_->GetFixutreCount();
        //FIXME 什么时候free ???????
        //sizeof(CGPoint):8,sizeof(vertexs)4
        NSLog(@"sizeof(CGPoint):%ld,sizeof(vertexs)%ld",sizeof(CGPoint),sizeof(vertexs));
        vertexs = (CGPoint*)malloc(sizeof(CGPoint)*fCount);
        NSLog(@"sizeof(CGPoint):%ld,sizeof(vertexs)%ld",sizeof(CGPoint),sizeof(vertexs));
        b2Fixture *fixture = body_->GetFixtureList();
        int leng=sizeof(vertexs)/sizeof(CGPoint);
        int i=0;
        while( fixture != nil ) {            
            b2Shape::Type t = fixture->GetType();
            if( t ==  b2Shape::e_edge ) {
                b2EdgeShape *rail = dynamic_cast<b2EdgeShape*>(fixture->GetShape());
                CGPoint origin =  ccp(rail->m_vertex1.x*kPhysicsPTMRatio,rail->m_vertex1.y*kPhysicsPTMRatio) ;
                CGPoint destination = ccp(rail->m_vertex2.x*kPhysicsPTMRatio, rail->m_vertex2.y*kPhysicsPTMRatio);
                //线段，中间节点会重复递归，因此这里至获得首节点
                memcpy(&vertexs[i],&origin, sizeof(CGPoint));
                
                if ((i+1)==(fCount-1)) {
                    //如果是最后一个，则存储尾节点
                    memcpy(&vertexs[i+1],&destination, sizeof(CGPoint));
                }
            }
            i++;
            fixture = fixture->GetNext();
        }
        printf("vertexs leng:%d\n, fixture count:%d\n",leng,fCount);
        for (int j=0; j<fCount; j++) {
            printf("vertexs[%d] %f,%f \n",j,vertexs[j].x,vertexs[j].y);
        }
        
     	}
	return self;
}
-(void)draw{
    NSLog(@"Railing draw-----");
    [super draw];
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position |kCCVertexAttribFlag_Color);
    ccDrawColor4F(1.0, 0.0, 0.0, 1.0);
    glLineWidth(2.0f);

    for (int i=0; i<fCount; i++) {
        CCTexture2D *texture = [self texture];        
        ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );        
        glLineWidth(10);
        glBindTexture(GL_TEXTURE_2D, [texture name]);        
        glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, &vertexs[i]);
        glDrawArrays(GL_LINES, 0, 2);
    }

}
@end
